﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

namespace DotNetExtensions.Core.Image
{
    public class Converter
    {
        public static Stream Compress(string filename, int quality = 50, float scale = 0.5f)
        {
            System.Drawing.Image source = System.Drawing.Image.FromFile(filename);
            ImageFormat format = source.RawFormat;
            int sW = 0, sH = 0;

            Size size = new Size(source.Width, source.Height);
            int dHeight = (int)(size.Height * scale);
            int dWidth = (int)(size.Width * scale);
            if (size.Width > dHeight || size.Width > dWidth)
            {
                if ((size.Width * dHeight) > (size.Height * dWidth))
                {
                    sW = dWidth;
                    sH = dWidth * size.Width / size.Height;
                }
                else
                {
                    sH = dHeight;
                    sW = size.Width * dHeight / size.Height;
                }
            }
            else
            {
                sW = size.Width;
                sH = size.Height;
            }

            Bitmap bitmap = new Bitmap(dWidth, dHeight);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(source, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel);
            g.Dispose();
            EncoderParameters parameters = new EncoderParameters();

            EncoderParameter parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { quality });
            parameters.Param[0] = parameter;
            Stream stream = new MemoryStream();
            try
            {
                ImageCodecInfo[] imageCodecInfos = ImageCodecInfo.GetImageEncoders();

                ImageCodecInfo imageCodecInfo = null;

                for (int x = 0; x < imageCodecInfos.Length; x++)
                {
                    if ("JPEG".Equals(imageCodecInfos[x].FormatDescription))
                    {
                        imageCodecInfo = imageCodecInfos[x];
                        break;
                    }
                }
                if (imageCodecInfo != null)
                {
                    bitmap.Save(stream, imageCodecInfo, parameters);
                }
                else
                {
                    bitmap.Save(stream, format);
                }
                stream.Seek(0, SeekOrigin.Begin);
                return stream;
            }
            catch
            {
                return null;
            }
            finally
            {
                source.Dispose();
                bitmap.Dispose();
            }
        }
    }
}
